<%@ Page Language="C#" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Configuration" %>
<%@ import Namespace="System.Collections" %>
<%@ import Namespace="System.Collections.Specialized" %>
<%@ import Namespace="System.Data" %>
<script runat="server">

    //Based on Alex Lowe's CacheViewer: http://aspalliance.com/aldotnet/examples/cacheviewer.aspx
    
         private void Page_Load(object sender, System.EventArgs e)
                 {
                     if(!IsPostBack)
                     {
                         hlRefresh.NavigateUrl = Request.RawUrl;
                         //In a cluster, it might be nice to know which machine we are on.
                         litMachine.Text = System.Environment.MachineName;
    
                         //Bind CachedItems
                         BindGrid();
                     }
                 }
    
   
                 //Bind the grid with items found in the cache.
                 private void BindGrid()
                 {
                     //Create an arraylist to hold our cacheditem details
                     ArrayList al = new ArrayList();
                     //Get enumerator
                     IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
    
                     //loop
                     while (CacheEnum.MoveNext())
                     {
                         try
                         {
                             //Do we want to include this item
                             if(IncludeInList(CacheEnum.Key.ToString()))
                             {
                                 int count = 0;
								 IList il = CacheEnum.Value as IList;
                                 if (il == null)
                                 {
                                     if (CacheEnum.Value is IDictionary)
                                     {
                                         count = (CacheEnum.Value as IDictionary).Count;
                                     }
                                 }
                                 else
                                 {
                                     count = il.Count;
                                 }
                                 al.Add(new CachedItem(CacheEnum.Key.ToString(), CacheEnum.Value.GetType().ToString(), count));
                             }
                         }
                         catch{}
                     }
                     //set counter Literal
                     litCount.Text = al.Count.ToString();
    
                     //Bind
                     dgCachedItems.DataSource = al;
                     dgCachedItems.DataBind();
    
                 }
    
                 //Remove item found on this row
                 protected void Grid_ItemCommand(object sender, DataGridCommandEventArgs e)
                 {
                     Literal l = (Literal)e.Item.FindControl("CacheItemName");
                     //make sure we found something :)
                     if(l != null)
                     {
                         Cache.Remove(l.Text);
                     }
                     this.BindGrid();
                 }
    
                 //Add quick counter column, probably could have done this in CachedItem as well
                  protected void Grid_Created(object sender, DataGridItemEventArgs e)
                  {
                     if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                     {
                            ((Literal)e.Item.FindControl("Counter")).Text = (e.Item.ItemIndex + 1).ToString();
                     }
                  }
    
                 //We only want to include what we put in the cache
                 private bool IncludeInList(string text)
                 {
                     if(string.Compare(text,0,"ISAPIWo",0,7,true) != 0)
                     {
                         return (string.Compare(text,0,"System",0,6,true) != 0);
                     }
                     return false;
                 }
    
                 //quick holding item to bind grid with
                 private class CachedItem
                 {
    
                     public CachedItem(){}
    
                     public CachedItem(string key, string type, int count)
                     {
                         this.CacheKey = key;
                         this.CacheType = type;
						 this._count = count.ToString();
                     }

                     public CachedItem(string key, string type):this(key,type,0)
					 {}
    
                     private string _cacheKey;
                     public string CacheKey
                     {
                         get {return this._cacheKey;}
                         set {this._cacheKey = value;}
                     }
    
                     private string _cacheType;
                     public string CacheType
                     {
                         get {return this._cacheType;}
                         set {this._cacheType = value;}
                     }

					private string _count;
					 public string Count
					 {
						get{return _count;}
					 }
                 }
    
                 //Remove All? Loop through collection removing each item we find
                 void lbRemoveAll_Click(object sender, EventArgs e)
                 {
                     IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
                         while (CacheEnum.MoveNext())
                         {
                             try
                             {
                                 if(IncludeInList(CacheEnum.Key.ToString()))
                                 {
                                     Cache.Remove(CacheEnum.Key.ToString());
                                 }
                             }
                             catch{}
                         }
                         //Rebind grid..although nothing will be there.
                         //Maybe just call Items.Clear();...maybe something new will be added
                         BindGrid();
                 }

</script>
<html>
<head>
</head>
<body>
    <form id="Form1" runat="server">
        <p>
            View (and remove) Cached Items 
            <br />
            Machine: 
            <asp:Literal id="litMachine" runat="server"></asp:Literal>
            <br />
            Count: 
            <asp:Literal id="litCount" runat="server"></asp:Literal>
            <br />
            <asp:LinkButton id="lbRemoveAll" onclick="lbRemoveAll_Click" runat="server">Clear Cache</asp:LinkButton>
        </p>
        <p>
            <asp:DataGrid id="dgCachedItems" runat="server" OnItemCommand="Grid_ItemCommand" AutoGenerateColumns="False" OnItemCreated="Grid_Created">
                <Columns>
                    <asp:TemplateColumn HeaderText="Count">
                        <ItemTemplate>
                            <asp:Literal id="Counter" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Name (Key)">
                        <ItemTemplate>
                            <asp:Literal id="CacheItemName" runat="server" text='<%# ((CachedItem)(Container.DataItem)).CacheKey %>' />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Data Type">
                        <ItemTemplate>
                            <asp:Literal id="CacheItemDataType" runat="server" text='<%# ((CachedItem)(Container.DataItem)).CacheType %>' />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Items">
                        <ItemTemplate>
                            <asp:Literal id="CacheItemDataCount" runat="server" text='<%# ((CachedItem)(Container.DataItem)).Count %>' />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                    <asp:TemplateColumn HeaderText="Remove">
                        <ItemTemplate>
                            <asp:LinkButton ID="RemoveButton" Text="Remove" CommandName="RemoveFromCache" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateColumn>
                </Columns>
            </asp:DataGrid>
        </p>
        <p>
            <asp:HyperLink id="hlRefresh" Text="Refresh" Runat="server"></asp:HyperLink>
        </p>
    </form>
</body>
</html>
